<!--
 * @Author: 冕上 cxm66888@163.com
 * @Date: 2024-10-21 14:34:55
 * @LastEditors: 冕上 cxm66888@163.com
 * @LastEditTime: 2024-12-24 11:40:46
 * @Description: 
 * 
 * Copyright (c) 2024 by xiaoMing.Cao, All Rights Reserved. 
-->
<!--
 * @Author: 冕上 cxm66888@163.com
 * @Date: 2024-10-15 16:07:18
 * @LastEditors: 冕上 cxm66888@163.com
 * @LastEditTime: 2024-10-21 11:42:12
 * @Description: 
 * 
 * Copyright (c) 2024 by xiaoMing.Cao, All Rights Reserved. 
-->
<template>
  <div class="content-main">
    <div class="m-center">
      <div class="c-list-main">
        <div v-for="item in data.list" :key="item._id" hover-class="list-hover">
          <!-- 聊天对话内容-右边 -->
          <div class="c-list c-l-r" v-if="isSelf(item)">
            <div class="c-list-right">
              <div class="c-l-r-bottom" :class="item.content_type == 'image' ? 'list-img' : ''">
                <img class="c-l-image" v-if="item.content_type == 'image'" :src="item.imgUrl" mode="widthFix"></img>
                <span class="c-l-r-span" v-else>{{ item.content }}</span>
                <!-- 是否已读 -->
                <span class="iconfont icon-yidu1" v-if="item.status == 'read'"></span>
              </div>
            </div>
            <div class="c-list-left">
              <img class="image" :src="item.sender_avatarUrl" mode="widthFix"></img>
            </div>
          </div>
          <!-- end -->
          <!-- 聊天对话内容-左边 -->
          <div class="c-list c-l-l" v-if="!isSelf(item)">
            <div class="c-list-left">
              <img class="image" :src="item.sender_avatarUrl" mode="widthFix"></img>
            </div>
            <div class="c-list-right">
              <div class="c-l-r-bottom" :class="item.content_type == 'image' ? 'list-img' : ''">
                <img class="c-l-image" v-if="item.content_type == 'image'" :src="item.imgUrl" mode="widthFix"></img>
                <span class="c-l-r-span" v-else>{{ item.content }}</span>
              </div>
            </div>
          </div>
          <!-- end -->
        </div>
      </div>
    </div>
    <div class="footer">
      <div class="footer-m">
      <textarea class="f-textarea" v-model="message" :focus="isFocus" name="" placeholder="请输入内容" @keydown="handleKeyDown" id=""></textarea>
      <div class="f-botto">
      <el-button type="danger" @click="sendPost(true)">不回复，清空未读</el-button>
      <el-button type="success" @click="sendPost(false)">发送</el-button>
    </div>
    </div>
    </div>
  </div>
</template>

<script setup>
  import { onMounted, ref, reactive } from 'vue';
  import { getMsgList, sendMsg } from '@/api/role';
  import { useRouter } from 'vue-router';
  const router = useRouter();

  // 获取id
  
  const data = reactive({
    // 真实瓶子
    list: [],
    form: {
      bottleStatus: '',
      bottleType: ''
    },
    total: 0
  });



  const isFocus = ref(false)

  const message = ref('')

  const handleKeyDown = (event) => {
      // 检查是否按下了 Command (Mac) 或 Control (Windows/Linux) 键以及 Enter 键
      if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {
        event.preventDefault(); // 阻止默认行为，例如换行

        // 在这里处理发送逻辑
        console.log('Sending:', message.value);
  sendPost()
        // 发送消息后清空文本框
        message.value = '';
      }
    };

    const sendPost = (isClean = false) => {
      if (!message.value && !isClean) {
        ElMessage.error('请输入内容');
        return;
      }
      sendMsg({
          content: message.value,
          windowId: parament.windowId,
          contentType: 'text',
          bottleId: currentUser.bottle_id,
          userId: currentUser.sender_userid,
          imgUrl: '',
          nickname: currentUser.sender_nickname,
          avatarUrl: currentUser.sender_avatarUrl,
          gender: currentUser.sender_gender,
          isClean,
        }).then(res => {
          if(res.code == 200) {
            ElMessage.success('发送成功');
            // 刷新列表
            getList();
             // 发送消息后清空文本框
              message.value = '';
          }
        })
    }
    
    onMounted(() => {
    isFocus.value = true;
  })
  const isSelf = (item) => {
    if (item.isMachine) {
      return true;
    }
    return false;
  }

  const parament = reactive({
    pageSize: 20,
    pageIndex: 1,
    windowId:''
  });

  parament.windowId = router.currentRoute.value.query.id;

  let currentUser = reactive({})

  const getList = () => {
    getMsgList(parament).then(res => {
      if (res.code == 200) {
        data.list = res.data?.data ?? [];
        data.total = res.data?.total ?? 0;

        for (let i=0; i <data.list.length; i++) {
          if (data.list[i].isMachine) {
            currentUser = Object.assign(currentUser, data.list[i])
          }
        }
      }
    });
  };

  const pageChange = item => {
    parament.pageIndex = item;
    getList();
  };

  getList();
</script>
<style scoped lang="scss">
  .bottle-mian {
    margin: 15px;
    background-color: #fff;
    padding: 15px;
  }
  .m-center{
    width: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    overflow: auto;
  }
  .content-main {
    width: 100%;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    align-items: center;
    position: relative;
    height: calc(100vh - 84px);
    .c-list-main {
      display: flex;
      flex-direction: column-reverse;
      width: 1000px;
    }

    .list-hover {
      opacity: 0.7;
    }

    .c-list {
      display: flex;
      justify-content: flex-start;
      padding: 10px 0;
      align-items: flex-start;

      .c-list-left {
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 0 15px;

        .image {
          width: 45px;
          height: 45px;
          border-radius: 50%;
        }
      }

      .c-list-right {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: flex-start;

        .c-l-r-top {
          display: flex;
          justify-content: space-between;
          align-items: center;
          width: 100%;

          .c-l-r-name {
            font-size: 15px; /* 30upx / 2 = 15px */
            color: #333;
          }

          .c-l-r-time {
            font-size: 12px; /* 24upx / 2 = 12px */
            color: #999;
          }
        }

        .c-l-r-bottom {
          display: flex;
          position: relative;
          .c-l-image {
            max-width: 200px; /* 400upx / 2 = 200px */
            border: 1px solid #e8e8e8; /* 2upx / 2 = 1px */
            border-radius: 8px; /* 16upx / 2 = 8px */
          }
          .c-l-r-span {
            color: #333;
            font-size: 16px; /* 32upx / 2 = 16px */
            padding: 10px 15px; /* 20upx / 2 = 10px, 30upx / 2 = 15px */
            line-height: 1.5;
            line-break: anywhere;
          }
          .icon-yidu1 {
            position: absolute;
            bottom: -2.5px; /* 5upx / 2 = 2.5px */
            left: -20px; /* 40upx / 2 = 20px */
            font-size: 16px; /* 32upx / 2 = 16px */
            color: #999;
          }
        }
      }
    }

    .c-l-l {
      .c-list-right {
        padding-right: 20%;
        display: flex;

        .c-l-r-bottom {
          background: #ffccc7;

          border-radius: 0 15px 15px 15px; /* 30upx / 2 = 15px */
          position: relative;

          .icon-yidu1 {
            left: auto;
            right: -20px; /* 40upx / 2 = 20px */
          }

          &::before {
            content: '';
            display: block;
            width: 10px; /* 20upx / 2 = 10px */
            height: 10px; /* 20upx / 2 = 10px */
            background: #ffccc7;
            position: absolute;
            left: -2.5px; /* 5upx / 2 = 2.5px */
            top: 0;
            border-radius: 0 15px 15px 15px; /* 30upx / 2 = 15px */
            transform: rotate(-10deg);
          }
        }
        .list-img {
          &::before {
            content: '';
            display: none;
          }
        }
      }
    }

    .c-l-r {
      .c-list-right {
        padding-left: 20%;
        padding-right: 0;
        display: flex;
        justify-content: flex-end;
        flex-direction: row;

        .c-l-r-bottom {
          background: #fff;
          color: #fff;
          border-radius: 15px 0 15px 15px; /* 30upx / 2 = 15px */
          position: relative;

          &::before {
            content: '';
            display: block;
            width: 10px; /* 20upx / 2 = 10px */
            height: 10px; /* 20upx / 2 = 10px */
            background: #fff;
            position: absolute;
            right: -2.5px; /* 5upx / 2 = 2.5px */
            top: 0;
            border-radius: 15px 0 15px 15px; /* 30upx / 2 = 15px */
            transform: rotate(10deg);
          }
        }
        .list-img {
          &::before {
            content: '';
            display: none;
          }
        }
      }
    }
  }

  .footer{
    width: 100%;
    border-top: 1px solid #dfdfdf;
    flex-shrink: 0;
    background-color: #fff;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    .footer-m{
      width: 1000px;
    }
    .f-botto{
      width: 100%;
      display: flex;
      justify-content: flex-end;
      padding: 15px 0;
    }
    .f-textarea{
      width: 1000px;
      height: 200px;
      padding: 15px;
      box-sizing: border-box;
      border: none;
      border-bottom: 1px solid #dfdfdf;
      margin: 0;
      outline: none;
    }
  }
</style>
